package com.administration;

import com.carpark.ejb.DAO.DAO;
import com.carpark.ejb.DAO.DaoImpl;
import com.carpark.ejb.Model.ClientsModel;
import com.webservice.DaoSingleObject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Sawior
 */
@ManagedBean
@ViewScoped
public class OnlineClients implements Serializable {

    private static final long serialVersionUID = 1L;
  private final DAO dao = getSingleDao();
    DaoSingleObject dso;
    private ClientsModel userSelectedByAdmin;

    public List<ClientsModel> getAllOnlineClients() {
        List<ClientsModel> allClients = dao.getAllClients();
        List<ClientsModel> onlineClients = new ArrayList<ClientsModel>();
        for (ClientsModel client : allClients) {
            if (client.getEmail() != null) {
                onlineClients.add(client);
            }
        }
        return onlineClients;
    }

    public void blockUser() {
        userSelectedByAdmin.setBlocked(true);
        dao.updateClient(userSelectedByAdmin);
        createMessage("Użytkownik zablokowany", "Użytkownik: " + userSelectedByAdmin.getEmail() + " został zablokowany");
        Logger.getLogger(OnlineClients.class.getName()).log(Level.SEVERE, "[CarPark] User: {0} has been blocked", userSelectedByAdmin);
    }

    public void unblockUser() {
        userSelectedByAdmin.setBlocked(null);
        dao.updateClient(userSelectedByAdmin);
        createMessage("Użytkownik odblokowany", "Użytkownik: " + userSelectedByAdmin.getEmail() + " został odblokowany");
        Logger.getLogger(OnlineClients.class.getName()).log(Level.SEVERE, "[CarPark] User: {0} has been unblocked", userSelectedByAdmin);
    }

    public ClientsModel getUserSelectedByAdmin() {
        return userSelectedByAdmin;
    }

    public void setUserSelectedByAdmin(ClientsModel userSelectedByAdmin) {
        this.userSelectedByAdmin = userSelectedByAdmin;
    }
  public DAO getSingleDao() {
        if (this.dso == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            this.dso = (DaoSingleObject) app.evaluateExpressionGet(context, "#{daoSingleObject}", DaoSingleObject.class);
        }
        return this.dso.getDao();
    }
    public void createMessage(String title, String msg) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(title, msg));
    }
}
